package com.ewhome.apartment.mvp.model;

import android.app.Application;

import com.ewhome.apartment.app.GlobalHttpHandlerImpl;
import com.ewhome.apartment.mvp.model.api.service.UserService;
import com.ewhome.apartment.mvp.model.entity.BaseData;
import com.ewhome.apartment.mvp.model.entity.res.ResApiBean;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewhome.apartment.mvp.contract.SplashContract;
import com.jess.arms.mvp.BaseModel;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@ActivityScope
public class SplashModel extends BaseModel implements SplashContract.Model {
    private Gson mGson;
    private Application mApplication;
    private UserService service;
    @Inject
    public SplashModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(UserService.class);
    }

    @Override
    public Observable<ResApiBean> getAllApis() {
        return service.apiCommon().map(new Function<BaseData, ResApiBean>() {
            @Override
            public ResApiBean apply(BaseData baseData) throws Exception {
                String data=GlobalHttpHandlerImpl.decodeFromJNI(baseData.getCipher());
                return mGson.fromJson(data,ResApiBean.class);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

}